package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public int minimumSubarrayLength(int[] nums, int k) {
        int len = nums.length;
        int[] kmap = new int[32];
        updateMap(kmap, k, 1);
        int l = 0;
        int r = 0;
        int min = Integer.MAX_VALUE;
        int[] map = new int[32];
        while (l <= r && r < len) {
            updateMap(map, nums[r++], 1);
            while (l <= r && getMapNum(map) >= k) {
                min = Math.min(min, r - l);
                updateMap(map, nums[l++], -1);
            }
        }
        if (min == 0) {
            return 1;
        }
        if (min == Integer.MAX_VALUE) {
            return -1;
        }
        return min;
    }

    public void updateMap(int[] map, int current, int incr) {
        for (int i = 0; i < 32; i++) {
            if ((current & (1 << i)) != 0) {
                map[i] += incr;
            }
        }
    }

    private int getMapNum(int[] map) {
        int current = 0;
        for (int i = 0; i < 32; i++) {
            if (map[i] > 0) {
                current |= (1 << i);
            }
        }
        return current;
    }

    public boolean canBeIncreasing(int[] nums) {
        int n = nums.length;
        int cnt = 0;
        for (int i = 1; i < n; i++) {
            if (nums[i - 1] >= nums[i]) {
                if (i > 1 && nums[i - 2] >= nums[i]) {
                    nums[i] = nums[i - 1];
                }
                cnt++;
                if (cnt > 1) {
                    return false;
                }
            }
        }
        return true;
    }
}
